--[[
FilePath: \Lua\View\UIActivityCenter\UIActivityChristmasTree.lua
周期兑换-圣诞树
--]]

require 'View/UIBase/UIBase'

UIActivityChristmasTree = UIBase.New(PanelResNames.UIActivityChristmasTree)
local _M = UIActivityChristmasTree

_M.hasInit = false
_M.activeId = nil
_M.costItemId = nil
_M.costNum = 0
_M.costName = nil
_M.hasEngNum = false
_M.isClick = false

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self.title = self:GetChild("title"):GetChild("title");
        self.txtScore = self:GetChild("TotaltaskInfo");
        self.BG = self:GetChild("BG");
        self.iconIcon = self:GetChild("icon");
        self.tipsText = self:GetChild("tips");
        self.list = self:SetDynamicList(self:GetChild("list_gift"),self.updateItem, nil , true);
        self.stageController = self:GetController("stage");
        self.ActTypeController = self:GetController("ActType");
        local costNode = self:GetChild("btnUse"):GetChild("cost");
        self.costIcon = costNode:GetChild("Image");
        self.txtCostNum = costNode:GetChild("num");
        self.CoinLb = self:GetChild("CoinLb");
        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self:RegisterOnClick(self:GetChild("Backbtn"),self.OnClose);
    self:RegisterOnClick(self:GetChild("btnUse"), self.OnUse);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
end

function _M:removeEvent()
end
-- 显示
function _M:OnEnable()
    if self.activeId == nil or not _M.hasInit then
        self:OnClose();
        return ;
    end
    self:Refresh();
end

function _M:Refresh()
    self.itemTableData, self.serverData, self.tableData = logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.tableData == nil or self.itemTableData == nil or self.serverData == nil or not self.serverData.isShow then
        self:OnClose();
        return ;
    end
    self.ActTypeController.selectedIndex = self.itemTableData.f_ActType;
    self.BG.url = self.itemTableData.f_bg
    local openState = logicMgr.ActiveManager.getActiveOpenStatus(self.activeId, 1);
    if openState ~= nil and openState then
        logicMgr.ActiveManager.updateActiveOpenStatus(self.activeId, 1);
    end
    if self.costItemId == nil then
        self.costItemId = self.itemTableData.f_ClickCost[1];
        local itemConfig = logicMgr.ItemManager.getItemConstDatabyModelId(self.costItemId);
        self.costIcon.url = EnumConst.AtlasPath.ItemAtlas .. itemConfig.icon;
        self.costNum = tonumber(self.itemTableData.f_ClickCost[2]);
        self.costName = itemConfig.name;
        self.CoinLb.icon = EnumConst.AtlasPath.ItemAtlas .. itemConfig.icon;
        self.iconIcon.icon = EnumConst.AtlasPath.ItemAtlas .. itemConfig.icon;
        if self.itemTableData.f_ActivityName ~= nil and self.itemTableData.f_ActivityName ~= "" then
            self.title.text = getLanguage(self.itemTableData.f_ActivityName);
        end
    end
    local haveNum = tonumber(logicMgr.ItemManager.getBagNumByTableID(self.costItemId));
    self.CoinLb.title = haveNum;
    if haveNum >= self.costNum then
        self.hasEngNum = true;
        self.txtCostNum.text = "[color=#D3D9DD]" .. self.costNum .. "[/color]";
    else
        self.hasEngNum = false;
        self.txtCostNum.text = "[color=#FF2A4D]" .. self.costNum .. "[/color]";
    end
    --self.title.text = getLanguage(self.tableData.f_ActivityName);
    self.txtScore.text = self.serverData.param.point;
    if self.stageController:HasPage(tostring(self.serverData.param.nowTime)) then
        self.stageController.selectedPage = tostring(self.serverData.param.nowTime);
    else
        self.stageController.selectedIndex = 0;
    end
    self.tipsText.text = getLanguage(self.itemTableData.f_ActFinishTips)
    for k, v in pairs(self.serverData.param.itemList) do
        if v.state == 0 then
            self.tipsText.text = getLanguage(self.itemTableData.f_ActGoingTips)
            break;
        end
    end
    self.list.numItems = #self.serverData.param.itemList;
    self.isClick = false;
    self.countDownNum = self.serverData.endTime - HelperFunc.GetServerTime() + 1;
    self:TimeCall(self.OnCountDown, 1000, -1);
end

function _M:updateItem(index, itemObj)
    local serverData = self.serverData.param.itemList[index];
    local progress = itemObj:GetChild("progress");
    local box = itemObj:GetChild("box");
    local statusController = box:GetController("status");
    local num = box:GetChild("num");
    progress.max = 100;
    progress.value = serverData.progress;
    num.text = serverData.cost
    statusController.selectedIndex = serverData.state
    box.onClick:Clear();
    if serverData.state == 1 then
        box.onClick:Add(function()
            self:OnGet(index);
        end);
    else
        box.onClick:Add(function()
            logicMgr.UIShowManager.ShowRewardPreviewByJson(serverData.award)
        end);
    end
end

--- 倒计时
function _M:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self:Refresh();
    else
    end
end

---宝箱领奖
function _M:OnGet(index)
    --if self.isClick then
    --    return;
    --end
    --self.isClick = true;
    ActivityHandler.sendReqActivityDrawRewardMessage({self.activeId, 2, index});
end

---装饰
function _M:OnUse()
    --if self.isClick then
    --    return;
    --end
    if self.serverData.param.hasGetAllReward then
        UISysTips.AddMsg(getLanguage("ActivityChristmasTreeTips1"));
        return;
    end
    if self.hasEngNum then
        --self.isClick = true;
        ActivityHandler.sendReqActivityDrawRewardMessage({self.activeId, 1, 1});
    else
        UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),self.costName));
    end
end

function _M:OnClose()
    self:OnBack();
end

-- 隐藏
function _M:OnDisable()

end
-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false;
    self.costItemId = nil;
    self.costNum = 0;
    self.costName = nil;
    self.hasEngNum = false;
    self.isClick = false;
end

return _M;